<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use app\admin\service\AddressService;
use think\Db;


class SysareaController extends AdminBaseController
{

    /**
     * 地区管理
     * @adminMenu(
     *     'name'   => '地区管理',
     *     'parent' => 'admin/Setting/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '地区管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {

        $dataAddress = new AddressService();
        $area_list = $dataAddress->getAreaList(); // 区域地址

        $list = $dataAddress->getProvinceList();

        foreach ($list as $k => $v) {
            if ($dataAddress->getCityCountByProvinceId($v['province_id']) > 0) {
                $v['issetLowerLevel'] = 1;
            } else {
                $v['issetLowerLevel'] = 0;
            }
            $list[$k]=$v;
            if (!empty($area_list)) {
                foreach ($area_list as $area) {
                    if ($area['area_id'] == $v['area_id']) {
                        $list[$k]['area_name'] = $area['area_name'];
                        break;
                    } else {
                        $list[$k]['area_name'] = "-";
                    }
                }
            }

        }
        $this->assign("area_list", $area_list);
        $this->assign("list", $list);

       return $this->fetch();
    }


    /**
     * ajax获取城市
     * @adminMenu(
     *     'name'   => 'ajax获取城市',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => 'ajax获取城市',
     *     'param'  => ''
     * )
     */
    public function selectCityListAjax()
    {
        if (request()->isAjax()) {
            $province_id = request()->post('province_id', '');
            $dataAddress = new AddressService();
            $list = $dataAddress->getCityList($province_id);
            foreach ($list as $v) {
                if ($dataAddress->getDistrictCountByCityId($v['city_id']) > 0) {
                    $v['issetLowerLevel'] = 1;
                } else {
                    $v['issetLowerLevel'] = 0;
                }
            }
            return $list;
        }
    }

    /**
     * ajax获取区域
     * @adminMenu(
     *     'name'   => 'ajax获取区域',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => 'ajax获取区域',
     *     'param'  => ''
     * )
     */
    public  function  selectdistrictlistajax(){
        if (request()->isAjax()) {
            $city_id = request()->post('city_id', '');
            $dataAddress = new AddressService();
            $list = $dataAddress->getDistrictList($city_id);
            return $list;
        }
    }

    /**
     * 添加一级地区
     * @adminMenu(
     *     'name'   => '添加一级地区a',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加一级地区',
     *     'param'  => ''
     * )
     */
    public  function addprovinceajax(){
        if (request()->isAjax()) {
            $dataAddress = new AddressService();
            $province_name = request()->post('regionName', ''); // 区域名称
            $sort = request()->post('regionSort', ''); // 排序
            $area_id = request()->post('area_id', 0); // 区域id
            $res = $dataAddress->addProvince($province_name, $sort, $area_id);
            return AjaxReturn($res);
        }
    }
    /**
     * 添加城市
     * @adminMenu(
     *     'name'   => '添加城市',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加城市',
     *     'param'  => ''
     * )
     */
    public function addcityajax(){
        if (request()->isAjax()) {
            $dataAddress = new AddressService();
            $city_id = 0;
            $province_id = request()->post('superiorRegionId', '');
            $city_name = request()->post('regionName', '');
            $zipcode = request()->post('zipcode', '');
            $sort = request()->post('regionSort', '');
            $res = $dataAddress->addOrupdateCity($city_id, $province_id, $city_name, $zipcode, $sort);
            return AjaxReturn($res);
        }
    }
    /**
     * 添加城市区域
     * @adminMenu(
     *     'name'   => '添加城市区域',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加城市区域',
     *     'param'  => ''
     * )
     */
    public function adddistrictajax(){
        if (request()->isAjax()) {
            $dataAddress = new AddressService();
            $district_id = 0;
            $city_id = request()->post('superiorRegionId', '');
            $district_name = request()->post('regionName', '');
            $sort = request()->post('regionSort', '');
            $res = $dataAddress->addOrupdateDistrict($district_id, $city_id, $district_name, $sort);
            return AjaxReturn($res);
        }
    }
    /**
     * 删除地区
     * @adminMenu(
     *     'name'   => '删除地区',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除地区',
     *     'param'  => ''
     * )
     */
    public function deleteregion(){
        if (request()->isAjax()) {
            $type = request()->post('type', '');
            $regionId = request()->post('regionId', '');
            $dataAddress = new AddressService();
            if ($type == 1) {
                $res = $dataAddress->deleteProvince($regionId);
                return AjaxReturn($res);
            }
            if ($type == 2) {
                $res = $dataAddress->deleteCity($regionId);
                return AjaxReturn($res);
            }
            if ($type == 3) {
                $res = $dataAddress->deleteDistrict($regionId);
                return AjaxReturn($res);
            }
        }
    }

    /**
     * 修改地区信息
     * @adminMenu(
     *     'name'   => '修改地区信息',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改地区信息',
     *     'param'  => ''
     * )
     */
    public function updateregionajax(){
        if (request()->isAjax()) {
            $dataAddress = new AddressService();
            $upType = request()->post('upType', '');
            $regionType = request()->post('regionType', '');
            $regionName = request()->post('regionName', '');
            $regionSort = request()->post('regionSort', '');
            $regionId = request()->post('regionId', '');
            $res = $dataAddress->updateRegionNameAndRegionSort($upType, $regionType, $regionName, $regionSort, $regionId);
            return AjaxReturn($res);
        }
    }
    /**
     * 修改省级信息
     * @adminMenu(
     *     'name'   => '修改省级信息',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改省级信息',
     *     'param'  => ''
     * )
     */
    public function updateProvinceAjax()
    {
        if (request()->isAjax()) {
            $dataAddress = new AddressService();
            $province_id = request()->post('eventId', '');
            $province_name = request()->post('regionName', '');
            $sort = request()->post('regionSort', '');
            $area_id = request()->post('area_id', '');
            $res = $dataAddress->updateProvince($province_id, $province_name, $sort, $area_id);
            return AjaxReturn($res);
        }
    }
    /**
     * 修改市级信息
     * @adminMenu(
     *     'name'   => '修改市级信息',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改市级信息',
     *     'param'  => ''
     * )
     */
    public function updateCityAjax()
    {
        if (request()->isAjax()) {
            $dataAddress = new AddressService();
            $city_id = request()->post('eventId', '');
            $province_id = request()->post('superiorRegionId', '');
            $city_name = request()->post('regionName', '');
            $zipcode = request()->post('zipcode', '');
            $sort = request()->post('regionSort', '');
            $res = $dataAddress->addOrupdateCity($city_id, $province_id, $city_name, $zipcode, $sort);
            return AjaxReturn($res);
        }
    }
    /**
     * 修改市级区域信息
     * @adminMenu(
     *     'name'   => '修改市级区域信息',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改市级区域信息',
     *     'param'  => ''
     * )
     */
    public function updateDistrictAjax()
    {
        if (request()->isAjax()) {
            $dataAddress = new AddressService();
            $district_id = request()->post('eventId', '');
            $city_id = request()->post('superiorRegionId', '');
            $district_name = request()->post('regionName', '');
            $sort = request()->post('regionSort', '');
            $res = $dataAddress->addOrupdateDistrict($district_id, $city_id, $district_name, $sort);
            return AjaxReturn($res);
        }
    }





}